﻿using Microsoft.Rtc.Collaboration;
using Microsoft.Rtc.Collaboration.ConferenceManagement;
using Microsoft.Rtc.Internal.Collaboration;
using Microsoft.Rtc.Signaling;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MSFT_SAMPLE_Scheduler
{
    class MeetingUser
    {

        public string userUri;
        private string _appID;
        public string serverFqdn;
        public bool verbose = true;
        private UserEndpoint _userEndpoint;
        private UCMAHelper _helper;




        public void Init()
        {
            this._appID = "sefa2";
            this._helper = new UCMAHelper();
           // try
           // {
                    this.Trace("Starting CollabPlatform...");
                    this._helper.CreateAndStartServerPlatform();
                    this.Trace("Registering User Endpoint...");
                    this._userEndpoint = this._helper.CreateUserEndpoint(new UserEndpointSettings(this.userUri, this.serverFqdn));
                    this._helper.EstablishUserEndpoint(this._userEndpoint);
                    this.Trace("Subscribing ...");
                    //this._userEndpoint.LocalOwnerPresence.PresenceNotificationReceived += new EventHandler<LocalPresentityNotificationEventArgs>(this.OnCategoryNotificationReceived);
                    //this._userEndpoint.LocalOwnerPresence.DelegateNotificationReceived += new EventHandler<DelegatesNotificationEventArgs>(this.OnDelegateNotificationReceived);
                    //this._helper.DoSelfSub(this._userEndpoint);
                    this.Trace("Init user: "+userUri);
                    
         /*   }
            catch (Exception exception)
            {
                this.Trace(exception.ToString());
            }*/
        }

        public void GetConference(SchedulingTemplate template)
        {
            this.Trace("Getting conferences");
            
            _userEndpoint.ConferenceServices.BeginGetConferenceSummaries(template,
            result =>
            {
                try
                {
                    Collection<ConferenceSummary> summaries =
                        _userEndpoint.ConferenceServices.EndGetConferenceSummaries(result);

                    foreach (ConferenceSummary summary in summaries)
                    {
                      
                            _userEndpoint.ConferenceServices.BeginGetConference(summary.ConferenceId,
                                resultFull =>
                                {
                                    Conference conferenceData = _userEndpoint.ConferenceServices.EndGetConference(resultFull); 
                                    try
                                    {
                                        Trace("*******");
                                        Trace("WebURL: "+conferenceData.WebUrl);
                                        Trace("Expires: " + conferenceData.ExpiryTime);
                                        //Trace("Phone: " + conferenceData.PhoneInformation.PhoneAccess.AccessNumbers[0].Number);
                                        Trace("PIN: " + conferenceData.Passcode);
                                        
                                        Trace("*******");
                                        
                                    }
                                    catch (RealTimeException ex)
                                    {
                                        Trace(ex.ToString());
                                    }
                                },null);
                            Trace("----------------------------");
                    }
                }
                catch (RealTimeException ex)
                {
                    Trace(ex.ToString());
                }
            },null);

            
        }

        public void CreatStaticConference()
        {
            ConferenceScheduleInformation info = new ConferenceScheduleInformation(SchedulingTemplate.AdministratorSupplied);
            
            _userEndpoint.ConferenceServices.BeginScheduleConference(info,
            result =>
            {
                Conference conf = _userEndpoint.ConferenceServices.EndScheduleConference(result);
                Trace(conf.WebUrl);
            }, null);
        }


        public void CreatNewConference()
        {
            ConferenceParticipantInformation participant = new ConferenceParticipantInformation("sip:a182620@cloudontap.net", ConferencingRole.Leader);
            ConferenceScheduleInformation info = new ConferenceScheduleInformation();
            info.AccessLevel = ConferenceAccessLevel.Invited;
            info.Description = "Test Description";
            info.PhoneAccessEnabled = true;
            info.Subject = "Test Subject";
            info.AttendanceAnnouncementsStatus = AttendanceAnnouncementsStatus.Disabled;
            info.AutomaticLeaderAssignment = AutomaticLeaderAssignment.SameEnterprise;
            info.ExpiryTime = Convert.ToDateTime("5/1/2014");
            info.Participants.Add(participant);
            _userEndpoint.ConferenceServices.BeginScheduleConference(info,
            result =>
            {
                Conference conf = _userEndpoint.ConferenceServices.EndScheduleConference(result);
                Trace(conf.WebUrl);
            }, null);
        }

        public void CancelMeeting(string conferenceId)
        {
            _userEndpoint.ConferenceServices.BeginCancelConference(conferenceId,
            result =>
            {
                _userEndpoint.ConferenceServices.EndCancelConference(result);
                Trace("Removed:"+conferenceId);
            }, null);
        }

        public void CancelMeetingStatic(string conferenceId)
        {
            _userEndpoint.ConferenceServices.BeginCancelConference(conferenceId,SchedulingTemplate.AdministratorSupplied,
            result =>
            {
                _userEndpoint.ConferenceServices.EndCancelConference(result);
                Trace("Removed:" + conferenceId);
            }, null);
        }



        public void ResetUserStaticConference(string userURI)
        {

            _userEndpoint.ConferenceServices.BeginGetConferenceSummaries(SchedulingTemplate.AdministratorSupplied,
            result =>
            {
                try
                {
                    Collection<ConferenceSummary> summaries = _userEndpoint.ConferenceServices.EndGetConferenceSummaries(result);
                    if (summaries == null)
                    {
                        CreatStaticConference();
                        Trace("Doesnt Exist Creating static");
                    }
                    else
                    {
                        CancelMeetingStatic(summaries[0].ConferenceId);
                        CreatStaticConference();
                        Trace("Existed reset");
                    }
    
                }
                catch (RealTimeException ex)
                {
                    Trace(ex.ToString());
                }
            }, null);
        }

        public void Destroy()
        {
            try
            {
                this.Trace("shutdown");
                this._helper.ShutdownPlatform();
            }
            catch (Exception exception)
            {
                this.Trace(exception.ToString());
            }
            this.Trace("killed");
        }

        private void Trace(string msg)
        {
            if (this.verbose)
            {
                Console.Out.WriteLine(msg);
            }
        }
    
    
    }
}
